import { wxRequest } from "../../utils/httpRequest"
import utils from "../../utils/util"
import Notify from '@vant/weapp/notify/notify';

Page({
    data: {
        nowUserInfo: null,
        activeNames: [],
        showDialogFlag: false,
        dialogTitle: "",    // dialog的提示
        fieldValue: "", // 弹出框的input绑定
        addOrUpdate: 1, 
        // 1: addAddress  2: updateAddress 3:deleteAddress 
        // 4: addRemarks  5: updateRemarks 6:deleteRemarks
        // 7: changeRealName  8: changePhone
        originalValue: ""   // 原本的信息
    },
    // 获取当前用户的个人信息
    getNowUserInfo() {
      wxRequest('getOneUserInfo', {}).then((res) => {
        if (res.code == 10000) {
          this.setData({ nowUserInfo: res.data });
        }
      })
    },
    // 手风琴伸缩
    onChange(event) {
        this.setData({
          activeNames: event.detail,
        });
    },
    // dialog输入框的变换
    fieldValueChange(event) {
        this.setData({ fieldValue: event.detail });
    },
    // 根据addOrUpdate的数值判断进行增加还是修改
    doAddOrUpdate() {
        console.log(this.data.addOrUpdate);
        console.log("添加地址");
        if (this.data.addOrUpdate == 1) {
            this.doAddAddress();
        } else if (this.data.addOrUpdate == 2) {
            this.doUpdateAddress();
        } else if (this.data.addOrUpdate == 3) {
            this.doDeleteAddress();
        } else if (this.data.addOrUpdate == 4) {
            this.doAddRemarks();
        } else if (this.data.addOrUpdate == 5) {
            this.doUpdateRemarks();
        } else if (this.data.addOrUpdate == 6) {
            this.doDeleteRemarks();
        } else if (this.data.addOrUpdate == 7) {
            this.doChangeRealName();
        } else if (this.data.addOrUpdate == 8) {
            this.doChangePhone();
        }
    }, 
    // 添加地址信息
    addAddress() {
        this.setData({ dialogTitle: "新增地址信息" });
        this.setData({ addOrUpdate: 1 });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互添加地址信息
    doAddAddress() {
        let url = "doAddAddress";
        let params = {
            updateValue: this.data.fieldValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '添加成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 修改地址信息
    updateAddress(event) {
        this.setData({ dialogTitle: "修改地址信息" });
        this.setData({ addOrUpdate: 2 });
        this.setData({ fieldValue: event.currentTarget.dataset.originalvalue });
        this.setData({ originalValue: event.currentTarget.dataset.originalvalue });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互修改地址信息
    doUpdateAddress() {
        let url = "doUpdateAddress";
        let params = {
            originalValue: this.data.originalValue,
            updateValue: this.data.fieldValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '修改成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 删除地址信息
    deleteAddress(event) {
        this.setData({ dialogTitle: "删除地址信息" });
        this.setData({ addOrUpdate: 3 });
        this.setData({ fieldValue: "你确定要删除该地址吗?" });
        this.setData({ originalValue: event.currentTarget.dataset.originalvalue });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互删除地址信息
    doDeleteAddress() {
        let url = "doDeleteAddress";
        let params = {
            originalValue: this.data.originalValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '删除成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 与后端交互修改常用地址
    changeCommonAddress(event) {
        let url = "changeCommonAddress";
        let params = {
            originalValue: event.currentTarget.dataset.originalvalue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '修改默认地址成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 添加备注信息
    addRemarks() {
        this.setData({ dialogTitle: "新增备注信息" });
        this.setData({ addOrUpdate: 4 });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互添加备注信息
    doAddRemarks() {
        let url = "doAddRemarks";
        let params = {
            updateValue: this.data.fieldValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '添加成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 修改备注信息
    updateRemarks(event) {
        this.setData({ dialogTitle: "修改备注信息" });
        this.setData({ addOrUpdate: 5 });
        this.setData({ fieldValue: event.currentTarget.dataset.originalvalue });
        this.setData({ originalValue: event.currentTarget.dataset.originalvalue });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互修改备注信息
    doUpdateRemarks() {
        let url = "doUpdateRemarks";
        let params = {
            originalValue: this.data.originalValue,
            updateValue: this.data.fieldValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '修改成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 删除备注信息
    deleteRemarks(event) {
        this.setData({ dialogTitle: "删除备注信息" });
        this.setData({ addOrUpdate: 6 });
        this.setData({ fieldValue: "你确定要删除该备注吗?" });
        this.setData({ originalValue: event.currentTarget.dataset.originalvalue });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互删除地址信息
    doDeleteRemarks() {
        let url = "doDeleteRemarks";
        let params = {
            originalValue: this.data.originalValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '删除成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 与后端交互修改常用备注
    changeCommonRemarks(event) {
        let url = "changeCommonRemarks";
        let params = {
            originalValue: event.currentTarget.dataset.originalvalue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '修改默认备注成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 修改真实姓名
    changeRealName(event) {
        this.setData({ dialogTitle: "修改真实姓名" });
        this.setData({ addOrUpdate: 7 });
        this.setData({ fieldValue: event.currentTarget.dataset.originalvalue });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互修改真实姓名
    doChangeRealName() {
        let url = "doChangeRealName";
        let params = {
            updateValue: this.data.fieldValue
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '修改姓名成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 修改性别
    doChangeSex(event) {
        let url = "doChangeSex";
        let params = {                            
            updateValue: event.detail
        }
        wxRequest(url, params).then((res) => {
            if (res.code == 10000) {
                Notify({ type: 'success', message: '修改性别成功' });
                this.getNowUserInfo();
            }
        })
    },
    // 修改联系电话
    changePhone(event) {
        this.setData({ dialogTitle: "修改手机电话" });
        this.setData({ addOrUpdate: 8 });
        this.setData({ fieldValue: event.currentTarget.dataset.originalvalue });
        this.setData({ showDialogFlag: true });
    },
    // 与后端交互修改联系电话
    doChangePhone() {
      let url = "doChangePhone";
      let params = {
          updateValue: this.data.fieldValue
      }
      wxRequest(url, params).then((res) => {
          if (res.code == 10000) {
              Notify({ type: 'success', message: '修改手机电话成功' });
              this.getNowUserInfo();
          }
      })
    },
    // dialog关闭时的收尾工作
    onClose() {
        console.log("取消添加地址");
        this.setData({ show: false });
        this.setData({ addOrUpdate: 1 });
        this.setData({ originalValue: "" });
        this.setData({ fieldValue: "" });
        this.setData({ dialogTitle: "" });
    },
    onLoad(options) {
      this.getNowUserInfo();
    },
})